package com.liru.chat_demo.config;

import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.openai.OpenAiChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiConfig {

    @Bean
    OpenAiChatModel openAiChatModel() {
        return OpenAiChatModel.builder()
                .apiKey("sk-f59b2d769df145f4b9b64e72eb5220aa")
                .modelName("qwen-flash")
                .logRequests(true)
                .logResponses(true)
                //.timeout(Duration.ofSeconds(10))
                .maxRetries(3)
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();
    }

    @Bean
    ChatMemoryProvider chatMemoryProvider() {
        return memoryId -> MessageWindowChatMemory.builder()
                .maxMessages(10)
                .id(memoryId)
                .build();
    }
/*    @Bean
    ContentRetriever contentRetriever(EmbeddingStore<TextSegment> store) {
        return EmbeddingStoreContentRetriever.builder()
                .embeddingStore(store)
                .minScore(0.7)
                .maxResults(5)
                .build();
    }*/

/*    @Bean
    ContentRetriever contentRetriever(MilvusEmbeddingStore embeddingStore) {
        EmbeddingModel embeddingModel = new AllMiniLmL6V2EmbeddingModel();
        DocumentSplitter documentSplitter = new DocumentByParagraphSplitter(1000, 0);
        EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor.builder()
                .documentSplitter(documentSplitter)
                .embeddingModel(embeddingModel)
                .embeddingStore(embeddingStore)
                .build();
        // 这里写你们自己准备的文件的绝对路径（我的路径不一定是你的路径）
        List<Document> documents = FileSystemDocumentLoader.loadDocuments("D:\\Code idea\\chat_demo\\src\\main\\resources\\templates");
        ingestor.ingest(documents);
        return EmbeddingStoreContentRetriever.builder()
                .embeddingStore(embeddingStore)
                .embeddingModel(embeddingModel)
                .minScore(0.7)
                .maxResults(5)
                .build();
    }*/

/*    @Bean
    MilvusEmbeddingStore embeddingStore() {
        return MilvusEmbeddingStore.builder()
                .host("localhost")
                .port(19530)
                .collectionName("my_database_0")
                .dimension(384)
                .indexType(IndexType.FLAT)
                .metricType(MetricType.COSINE)
                .consistencyLevel(ConsistencyLevelEnum.EVENTUALLY)
                .autoFlushOnInsert(true)
                .idFieldName("id")
                .textFieldName("text")
                .metadataFieldName("metadata")
                .vectorFieldName("vector")
                .build();
    }*/

/*    @Bean
    MilvusClientV2 milvusClient() {
        ConnectConfig config = ConnectConfig.builder()
                .uri("http://localhost:19530")
                .token("root:Milvus")
                .build();
        MilvusClientV2 client = new MilvusClientV2(config);
        CreateDatabaseReq createDatabaseReq = CreateDatabaseReq.builder()
                .databaseName("my_database_1")
                .build();
        return client;
    }*/

}
